package kook.domain.view;

import lombok.Data;

import java.io.Serializable;
import java.util.Objects;

@Data
public class ResultView implements Serializable {
    /**
     * 规范参考 https://blog.csdn.net/u011368943/article/details/78072973
     * 或者 https://www.cnblogs.com/chenweichu/p/6910667.html
     */
    private int code;
    private String msg;
    //@JsonInclude(JsonInclude.Include.NON_NULL)
    private Object data;
    private final static int DEFAULT_CODE = 0;
    private final static String DEFAULT_MSG = "ok";

    //private final static Object DEFAULT_DATA = new Object();
    private ResultView() {
    }

    public static ResultView success() {
        ResultView resultView = new ResultView();
        resultView.setCode(DEFAULT_CODE);
        resultView.setMsg(DEFAULT_MSG);
//        resultView.setData(DEFAULT_DATA);
        return resultView;
    }

    public static ResultView success(Object data) {
        ResultView resultView = new ResultView();
        resultView.setCode(DEFAULT_CODE);
        resultView.setMsg(DEFAULT_MSG);
        resultView.setData(data);
        return resultView;
    }

    public static ResultView fail(int code, String msg) {
        ResultView resultView = new ResultView();
        resultView.setCode(code);
        resultView.setMsg(msg);
        return resultView;
    }

    public static ResultView fail(int code, String msg, Object data) {
        ResultView resultView = new ResultView();
        resultView.setCode(code);
        resultView.setMsg(msg);
        resultView.setData(data);
        return resultView;
    }

    @Override
    public String toString() {
        return "ResultView("
                .concat("code=").concat(Objects.toString(code))
                .concat(", msg=").concat(Objects.toString(msg))
                .concat(", data=").concat(Objects.toString(data))
                .concat(")");
    }
}